var dataDetail = null;

;layui.define(function(e) {
    layui.use(['table','layer','form','laydate','util'], function(){
        var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;
        //展示已知数据
        table.render({
            elem: '#idTable'
            ,id: 'listReload'
            ,url: hostUrl+'/clue/platformClueList'
            ,cellMinWidth: 120
            ,loading: true
            ,cols: [[ //标题栏
//                {type:'checkbox', fixed: 'left'},
                {field: 'name', title: '客户姓名'}
                ,{field: 'sex', title: '性别',templet: function(d){
                	if(d.sex == '0'){
	                    return '男';
                	}else if(d.sex == '1'){
	                    return '女';
                	}else{
	                    return '-';
                	}
                }}
                ,{field: 'phone', title: '手机号', minWidth: 120}
                ,{field: 'city', title: '城市'}
                ,{field: 'type', title: '客户类型',templet: function(d){
                	if(d.type == '1'){
	                    return '个人客户';
                	}else if(d.type == '2'){
	                    return '企业客户';
                	}else{
	                    return '-';
                	}
                }}
                ,{field: 'storeName', title: '门店'}
                ,{field: 'car_info', title: '意向车型'}
                ,{field: 'create_time', title: '建档日期', minWidth: 180,templet: function(d){
                	return layui.util.toDateString(d.create_time, 'yyyy-MM-dd HH:mm:ss');
                }}
                ,{field: 'clue_source_type', title: '信息来源',templet: function(d){
                	if(d.clue_source_type == '0'){
	                    return '线上App';
                	}else if(d.clue_source_type == '1'){
	                    return '线上H5分享';
                	}else if(d.clue_source_type == '2'){
	                    return '平台建档';
                	}else if(d.clue_source_type == '3'){
	                    return '门店建档';
                	}else if(d.clue_source_type == '4'){
	                    return '销售顾问建档';
                	}else if(d.clue_source_type == '5'){
	                    return '经纪人推荐';
                	}else if(d.clue_source_type == '6'){
	                    return '直营店开业（抽奖活动H5）';
                	}else{
	                    return '其它';
                	}
                }}
                ,{field: 'brokerName', title: '经纪人'}
                ,{field: 'tool',title: '操作',fixed: 'right', minWidth:180, align:'center', toolbar: '#barDemo'}
            ]]
            ,page: true //是否显示分页
        });
       
        form.on('select(selectStore)', function(data){
        	selectStore();
        });
        
      	//监听工具条
        table.on('tool(listTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'distribute'){
            	dataDetail=data;
            	var detail=layer.open({
                    type: 2
                    ,id: (new Date()).valueOf()
                    ,title: '分配线索'
                    ,content: hostUrl+'/clue/distribution/platformDistribute.do'
                    ,area:  ['500px', '500px']
                    ,maxmin: true
                    ,end: function(){
                    	dataDetail=null;
                    	location.reload();
                    }
                }); 
            	layer.full(detail); 
            }else if (obj.event === 'culeDel'){
                layer.confirm('该线索信息确定要删除吗?', function(index){
                	$.axse(hostUrl+"/clue/delClue?clueId="+data.id,
                  		  null,
                  		function(s){
                      	  	if(s.data >= 1){
                  	   		    Layer.showSucMsg();
                  		   		setTimeout(function(){
//                  			 		window.location.href = hostUrl+"/clue/toBeAssigned/platformList.do";
                  		   				location.reload();
                  				},1000);
                  		   			return ;
                      	  	}else{
                     		       Layer.showFailMsg(s.msg);
                     		       	return ;
                      	    }
                  		},
                  		function(){
                  		     Layer.showFailMsg("删除失败");
                  		     	return ;
                  		});
                    layer.close(index);
                });
            }
        });
        
        $("#dowExcel").click(function () {
		    return window.location.href=hostUrl+"/clueFlow/dowExcel?name="+$("#name").val()+"&phone="+$("#phone").val()
		    +"&createTimeStart="+$("#start").val()+"&createTimeEnd="+$("#end").val()+"&accType="+$("#accType").val()
		    +"&clueSourceType="+$("#clueSourceType").val();
        })
        
        $("#inputOptions").click(function () {
            var index = layer.open({
                type: 2
                ,id: (new Date()).valueOf()
                ,title: '新建线索'
                ,content: hostUrl+'/clue/newClue/platformNewClue.do'
                ,area: ['500px', '500px']
                ,maxmin: true
                ,end: function(){
                	location.reload();
                }
            });
            layer.full(index);
        });
        //表刷新方法
        var reloadTable = function () {
            table.reload("listReload", { //此处是上文提到的 初始化标识id
                where: {
                    name: $("#name").val(),
                    phone: $("#phone").val(),
                    createTimeStart: $("#start").val(),
                    createTimeEnd: $("#end").val(),
                    accType: $("#accType").val(),
                    clueSourceType: $("#clueSourceType").val()
                }
            });
        };
        $("#search-button").on("click",function () {
            reloadTable();
        });
    });
    
    layui.use(['laydate'], function(){
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#start'
        });
        laydate.render({
            elem: '#end'
        });
        
    });
});